$(function () {
	getNodeList();
});

function getNodeList(){
	JY.Model.loading();
	JY.Ajax.doRequest("baseForm",jypath +'/backstage/workflow/process/nodeList',{"processDefinitionId":processDefinitionId,projectId:projectId},function(data){
		 $("#baseTable tbody").empty();
    	 var results=data.obj;
    	 var html="";
		 if(results!=null&&results.length>0){
    		 for(var i = 0;i<results.length;i++){
        		 var l=results[i];
        		 html+="<tr>";
        		 html+="<td class='center hidden-480'>"+JY.Object.notEmpty(l.nodeName)+"</td>";
        		 html+="<td class='center '>"+JY.Object.notEmpty(l.nodeUserName)+"</td>";
        		 html+="<td class='center'><a href='javascript:setNodeUser(&apos;"+l.id+"&apos;,&apos;"+l.nodeUserId+"&apos;,&apos;"+l.nodeRoleId+"&apos;)'>设置人员</a></td>";
        		 html+="</tr>";		 
        	 } 
    		 $("#baseTable tbody").append(html);
    	 }else{
    		html+="<tr><td colspan='10' class='center'>没有相关数据</td></tr>";
    		$("#baseTable tbody").append(html);
    	 }		 
    	 JY.Model.loadingClose();
	 });
}


function loadRoleUser(){
	var roleId=$("#nodeUserForm select[name$='roleId']").val();
	if(roleId==""){
		$("#userId").empty();
		return;
	}
	if(roleId=="9"){
		JY.Ajax.doRequest(null,jypath+'/estate/project/findProjectRoleUser',{roleId:roleId,projectId:projectId},function(data){
			var obj=data.obj;
			var html = "";
			console.log(obj);
			for(var i=0;i<obj.length;i++){
				html+='<option value="'+JY.Object.notEmpty(obj[i].id)+'">'+JY.Object.notEmpty(obj[i].userName)+'</option>';
			}
			
			$("#userId").empty();
			$("#userId").append(html);
			
		});
	
	}else{
		JY.Ajax.doRequest(null,jypath+'/estate/project/findProjectRoleUser',{roleId:roleId,projectId:projectId},function(data){
			var obj=data.obj;
			var allRole='<option value="'+JY.Object.notEmpty(obj.id)+'">'+JY.Object.notEmpty(obj.userName)+'</option>';
			$("#userId").empty();
			$("#userId").append(allRole);
			
		});
	}

	
}

function setNodeUser(nodeId,nodeUserId,nodeRoleId){
	if(null!=nodeUserId && nodeUserId!="" && "null"!=nodeUserId  && null!=nodeRoleId && nodeRoleId!="" && "null"!=nodeRoleId && "undefined"!=nodeRoleId){
		$("#nodeUserForm select[name$='roleId']").prop("value",JY.Object.notEmpty(nodeRoleId));
		JY.Ajax.doRequest(null,jypath+'/estate/project/findProjectRoleUser',{roleId:nodeRoleId,projectId:projectId},function(data){
			var obj=data.obj;
			var allRole='<option value="'+JY.Object.notEmpty(obj.id)+'">'+JY.Object.notEmpty(obj.userName)+'</option>';
			$("#userId").empty();
			$("#userId").append(allRole);
			add(nodeId);
		});
	}else{
		add(nodeId);
	}

}

function add(nodeId){
	JY.Model.edit("nodeUserFormDiv","设置节点人员",function(){
		 var roleId=$("#nodeUserForm select[name$='roleId']").val();
		 var userId=$("#nodeUserForm select[name$='userId']").val();
		 /*
		  * 可将节点人员设置为空
		  * if(!JY.Object.notNull(roleId)){
			$("#roleAddForm select[name$='roleId']").tips({side:1,msg : "请选择！",bg:'#FF2D2D',time:1});
			return;
		 }
		 if(!JY.Object.notNull(userId)){
				$("#roleAddForm select[name$='userId']").tips({side:1,msg : "请选择！",bg:'#FF2D2D',time:1});
				return;
		}*/
		var url="/backstage/workflow/process/saveNodeUser";
		var that =$(this);
		JY.Ajax.doRequest("nodeUserForm",jypath +url,{
			processDefinitionId:processDefinitionId,
			nodeId:nodeId,
			projectId:projectId,
			remark:roleId
		},function(data){	
			that.dialog("close");  
			window.location.reload();
		}); 
	})
}

function goProcessBack(){
	var backUrl=jypath+"/estate/projectProcdef/index?menu=menu_0003_0002&projectId="+projectId;
	window.location.href=backUrl;
}